
<!-- 相机扫描 -->

<template>
	<view class="page">
		<camera device-position="back" :flash="flash" style="width: 100%; height: 100%" mode="scanCode"
			@scancode="scancode" @error="error"></camera>
		<view class="mask-layer">
			<view class="scan-box"></view>
		</view>

		<view class="bottom-buttons">
			<view class="button" @click="changeFlash">
				<text>手机筒</text>
			</view>
			<view class="button" @click="onAlbum">
				<text>相册</text>
			</view>
		</view>

	
	</view>
</template>

<script>
	export default {
		name: "ScanPage",
		data() {
			return {
				flash: "auto",
			};
		},
		methods: {
			changeFlash() {
				this.flash = this.flash === "auto" ? "torch" : "auto";
			},
			scancode(e) {
				console.log("扫码结果：", e);
			},
			error(e) {
				console.error("相机错误：", e);
			},
			onCamera() {
				uni.showToast({
					title: "点击手机筒",
					icon: "none"
				});
			},
			onAlbum() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.tempImagePath = res.tempFilePaths[0];
					}
				});
			},
			upload() {
				uni.showToast({
					title: "上传中",
					icon: "loading"
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.page {
		width: 100%;
		height: 100%;
		position: relative;
		background-color: #000;
		box-sizing: border-box;
		overflow: hidden;
	}

	/* 灰色遮罩 */
	.mask-layer {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 5;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/* 扫码框（中间透明） */
	.scan-box {
		width: 500rpx;
		height: 500rpx;
		background-color: transparent;
		border: 2rpx solid #fff;
		box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
		pointer-events: none;
		z-index: 6;
	}

	/* 闪光灯按钮 */
	.flash {
		position: absolute;
		top: 240rpx;
		right: 80rpx;
		z-index: 10;

		&_img {
			width: 60rpx;
			height: 60rpx;
		}

		&.torch::before {
			content: "";
			position: absolute;
			width: 100%;
			height: 2rpx;
			background-color: #fff;
			transform: rotate(45deg);
			left: 0;
			top: 42%;
		}
	}

	/* 底部按钮组 */
	.bottom-buttons {
		position: absolute;
		bottom: 80rpx;
		width: 100%;
		display: flex;
		justify-content: space-around;
		z-index: 10;
	}

	.button {
		width: 100rpx;
		height: 100rpx;
		background-color: #fff;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 26rpx;
		color: #000;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.3);
	}

	/* 预览图样式（可自定义） */
	.image {
		position: absolute;
		bottom: 200rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 300rpx;
		height: 300rpx;
		border: 2rpx solid #fff;
		z-index: 20;
	}

	.check-box {
		position: absolute;
		bottom: 100rpx;
		left: 50%;
		transform: translateX(-50%);
		display: flex;
		gap: 60rpx;
		z-index: 20;
	}

	.icon {
		width: 60rpx;
		height: 60rpx;
	}
</style>